#ifndef PROJECT_LOG_H
#define PROJECT_LOG_H

#include <cstdio>
#include <ctime>
#include <pthread.h>
#include <iostream>
#include <cassert>
#include <vector>
#include <cstdint>
#include <cstring>
#include <unistd.h>

#define INF 0
#define DBG 1
#define ERR 2
#define LOG_LEVEL ERR

const int DefaultFD = -1;

#define LOG(level, format, ...)                                                                                        \
    do                                                                                                                 \
    {                                                                                                                  \
        if (level < LOG_LEVEL)                                                                                         \
            break;                                                                                                     \
        time_t t = time(NULL);                                                                                         \
        struct tm *ltm = localtime(&t);                                                                                \
        char tmp[32] = {0};                                                                                            \
        strftime(tmp, 31, "%H:%M:%S", ltm);                                                                            \
        fprintf(stdout, "[%p %s %s:%d] " format "\n", (void *)pthread_self(), tmp, __FILE__, __LINE__, ##__VA_ARGS__); \
    } while (0)

#define INF_LOG(format, ...) LOG(INF, format, ##__VA_ARGS__)
#define DBG_LOG(format, ...) LOG(DBG, format, ##__VA_ARGS__)
#define ERR_LOG(format, ...) LOG(ERR, format, ##__VA_ARGS__)

enum class SockExitCode
{
    CRE_SOCKET_FAIL,
    BIND_ADDR_FAIL,
    LISTEN_FAIL,
    CONN_SERVER_FAIL,
    ACCEPT_FAIL,
    RECV_FAIL,
    SEND_FAIL,
    REUSE_ADDR_FAIL,
    REUSE_PORT_FAIL,
    GET_SOCK_FLAG_FAIL,
    SET_SOCK_NBLOCK_FAIL,
};

enum class PollerExitCode
{
    CRE_EPOLLFD_FAIL = 11,
    EPOLL_CTL_ADD_FAIL,
    EPOLL_CTL_DEL_FAIL,
    EPOLL_CTL_MOD_FAIL,
};

enum class EventLoopExitCode
{
    CREATE_EVENTFD_FAILED = 15,
    WAKEUP_EVENTFD_FAILED,
    READ_EVENTFD_FAILED
};

enum class TimeWheelExitCode
{
    CREATE_TIMERFD_FAILED = 18,
    READ_TIMERFD_FAILED
};

#endif